'''
Created on Apr 13, 2011

@author: lrg
'''

class ModelCodeGen(object):
    '''
    I want to use this class to generate Halogen specific db model codes
    '''


    def __init__(self,modelClassName,pk,columns):
        '''
        Constructor
        '''
        self.modelClassName = modelClassName
        self.pkList = pk
        self.columns = columns
        self.output = open(self.modelClassName+".java", 'w')
        
    def printPK(self):
        print self.pkList
        
    def generate(self):
        
        self.write (0,"public class " + self.modelClassName + " extends PersistableObject{ \n")
        self.write (1,"private static final long serialVersionUID = 1L;")
        self.genFields()
        self.genGetterSetters()
        self.gen_isDataChanging()
        self.gen_updateDataAttributes()
        self.write (0,"}")

    def gen_updateDataAttributes(self):
        self.write(1,"@Override")
        self.write(1,"public void updateDataAttributes(Object obj, int modifyUserId){")
        self.write(2,"if (obj instanceof %s) {" %(self.modelClassName))
        self.write(3,"%s %s = (%s) obj;" % (self.modelClassName,self.modelClassName.lower(),self.modelClassName))
        for field in self.columns.keys():
            if (field not in self.pkList):
                self.write(3,"this.set%s(%s.get%s());" %(field.title(),self.modelClassName.lower(),field.title()))
        self.write(2,"}")
        self.write(1,"}")
        
        
        
    def gen_isDataChanging(self):
        self.write(1,"@Override")
        self.write(1,"public void isDataChanging(Object obj){")
        self.write(2,"boolean retVal = false;")
        self.write(2,"if(obj instanceof %s) {" %(self.modelClassName))
        self.write(3,"%s %s = (%s)obj;" %(self.modelClassName,self.modelClassName.lower(),self.modelClassName))
        self.write(3,"if (")
        diffList=[]
        for field in self.columns.keys():
            if (field not in self.pkList):
                diffList.append("Diff.isDifferent(get%s(),%s.get%s())" %(field.title(),self.modelClassName.lower(),field.title()))
        self.write(4,("\n"+"\t"*4+"||").join(diffList))
        self.write(3,"){")
        self.write(4,"returnValue = true;")
        self.write(3,"}")
        self.write(2,"}")
        self.write(2,"return retVal;")
        self.write(1,"}")
        
    def genFields(self):
        for field in self.columns.keys(): 
            self.write (1,"private " + self.columns[field]+" "+field+";")
            
    def genGetterSetters(self):
        self.write (0,"");
        for field in self.columns.keys():
            self.write (1,"public void set%s(%s %s){" %(field.title(),self.columns[field],field))
            self.write (2,"this.%s = %s;" %(field,field))
            self.write (1,"}\n")
            
            self.write (1,"public %s get%s(){" %(self.columns[field],field.title()))
            self.write (2,"return this.%s;" %(field))
            self.write (1,"}\n")
    
    def write(self,k,s):    
        self.output.write("\t"*k + s + "\n");
